package com.key.puzzlemodel.puzzlephoto

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.key.puzzlemodel.R
import com.key.puzzlemodel.databinding.ItemMediaCheckedLayoutBinding
import com.key.puzzlemodel.util.RecyclerMoveListener
import com.key.puzzlemodel.util.VibratorUtil
import java.util.*

/**
 *
 * @功能描述:
 * @创建时间:     2022/1/4
 * @创建者:       阳
 * @修改时间:     2022/1/4
 * @修改者:       阳
 */
class MediaCheckedAdapter(val mData: ArrayList<String>) :
    RecyclerView.Adapter<MediaCheckedAdapter.ViewHolder>(), RecyclerMoveListener {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.item_media_checked_layout,
                parent,
                false
            )
        )
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemBinding.tvMediaNum.text = (position + 1).toString()

        holder.itemBinding.partDelete.setOnClickListener {
            onItemRemove(position)
        }

        Glide.with(holder.itemBinding.cover.context).load(mData[position])
            .apply(RequestOptions().transforms(CenterCrop(), RoundedCorners(10)))
            .into(holder.itemBinding.cover)
    }

    override fun getItemCount(): Int {
        return mData.size
    }


    fun updateSelectPhoto(data: ArrayList<String>) {
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }

    fun addSelectPhoto(photo: String) {
        mData.add(photo)
        notifyDataSetChanged()
    }


    inner class ViewHolder(val itemBinding: ItemMediaCheckedLayoutBinding) :
        RecyclerView.ViewHolder(itemBinding.root) {


    }

    /**
     * 设置响应点击
     */
    fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }


    private var mOnItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onDelete(position: Int)
        fun onItemClick(position: Int)
        fun onMove()
    }

    /**
     * 手势操作时间
     */
    override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
        Collections.swap(mData, fromPosition, toPosition)
        notifyItemMoved(fromPosition, toPosition)
        return true
    }


    override fun onItemRemove(position: Int): Boolean {
        try {
            mData.removeAt(position)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        notifyDataSetChanged()
        if (null != mOnItemClickListener) {
            mOnItemClickListener!!.onDelete(position)
        }
        return true
    }

    override fun isLongPressDragEnabled(): Boolean {
        //是否允许长按拖拽
        return true
    }

    override fun isItemViewSwipeEnabled(): Boolean {
        return false
    }

    private var DRAG = false

    override fun clearView(viewHolder: RecyclerView.ViewHolder?, recyclerView: RecyclerView?) {
        if (DRAG) {
            //拖拽 刷新
            notifyDataSetChanged()
            if (null != mOnItemClickListener) {
                mOnItemClickListener!!.onMove()
            }
        }
        if (viewHolder != null) {
            (viewHolder as ViewHolder).itemBinding.flDrag.visibility = View.GONE
        }
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            DRAG = true
        }
        if (viewHolder != null) {
            VibratorUtil.onVibrator(viewHolder.itemView.context)
            (viewHolder as ViewHolder).itemBinding.flDrag.visibility = View.VISIBLE
        }
    }


}